package com.google.cbmgraph.cell;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;

import com.google.cbmgraph.data.*;
/**
 * This is the object that holds all of the datapoints it's a map of them
 * @author lawlerr1
 *
 */
public class DataMap {

	private ArrayList<int[]> datapoints;
	public int[][] myMap;
	private GraphCell[][] myNodeMap;
	private static final String TAG="DataMap";
	public static int xSize;
	public static int ySize;
	/**
	 * Constructor, get points from server and populates the map with them.
	 */
	public DataMap(){
		GraphData gd = new GraphData();
		datapoints = gd.getData();
		xSize = 74;
		ySize = 101;
		myMap = new int[xSize][ySize];
		myNodeMap = new GraphCell[xSize][ySize];
		int id=0;
		Log.v(TAG, "the size of the map is from 0 to "+xSize + " and 0 to "+ySize);
		for(int j=0; j<ySize; j++){
			for(int i=0; i<xSize; i++){
				myMap[i][j]=0;
				myNodeMap[i][j]=new GraphCell(id, CellFormat.EMPTYCELL);
				for (int k=0; k<datapoints.size(); k++){
					if (i == datapoints.get(k)[0] && j == datapoints.get(k)[1]){
						myMap[i][j]=1;
						myNodeMap[i][j].changeStatus(CellFormat.NODECELL);
//						Log.v(TAG, "index=" + i +", "+j+" contains a node");	
					}
				}
				if(myNodeMap[i][j].getStatus() != CellFormat.NODECELL){	
//					Log.v(TAG, "index=" + i +", "+j+" contains no nodes");
				}
				id++;
			}
		}

		calcConnections();
	}
/**
 * Helper function to find what empty cells need
 * to have a line drawn in them to act as connectors between points
 */
	private void calcConnections(){
		Log.v(TAG, "about to work on connections");
		Log.d(TAG, "the map is "+myNodeMap.length +" by "+ myNodeMap[0].length);
		// this loop marks all connections that need to be made...
		for (int j = ySize-1 ; j > 0; j--){
			for (int i = 0; i< xSize; i++){
//				Log.v(TAG, "Checking on :"+ i+", "+j);
				if (myNodeMap[i][j].getStatus() == CellFormat.NODECELL){
					Log.d(TAG, "Checking to see if connection is needed for node at "+i+", "+j);
					if (myNodeMap[i+1][j].getStatus() == CellFormat.NODECELL){
						Log.d(TAG, "No connection needed!");
						// do nothing
					}else{
						boolean nextFound = false;
						Log.d(TAG, "Connection needed!");
						// look for the next connection.
						for (int k = j; k < myNodeMap.length; k++){
							if (myNodeMap[i+1][k].getStatus() == CellFormat.NODECELL){
								nextFound = true;
								Log.v(TAG, "Found linking point!");
							}
						}
						if (nextFound){
							for (int k = j; k < myNodeMap.length; k++){
								myNodeMap[i][k].vertConnect= true;
								Log.d(TAG, "drawing connection!");
								if (myNodeMap[i][k].getStatus()== CellFormat.NODECELL){
									break;
								}
								
							}
						}
					}
				}
			}
		}
	}
	/**
	 * Returns if there's a node at a particular dimension or not
	 * @param x
	 * @param y
	 * @return
	 */
	public GraphCell getNode(int x, int y){
//		Log.v(TAG,"Is there a node here: "+x+", "+y+"?");
		if (x < myNodeMap.length && y < myNodeMap[0].length){
//			Log.v(TAG,"found a node here: "+x+", "+y+" with code: "+myNodeMap[x][y].getStatus());
			return myNodeMap[x][y];
		}
		else
//			Log.v(TAG,"Did not find a node here: "+x+", "+y);
			return new GraphCell((int)(Math.random()*10),CellFormat.NOTEDCELL);
	}
	/** 
	 * draws the coordinates on the axses  
	 * @param x
	 * @param y
	 * @param myHeaderCounter
	 * @param canvas
	 */
	public void drawCoord(int x, int y, int myHeaderCounter, Canvas canvas) {
		Paint paint = new Paint();
		paint.setStyle(Paint.Style.FILL);
		paint.setAntiAlias(true);
		paint.setTextSize(12);
		paint.setColor(Color.WHITE);
		if (myHeaderCounter == -99){
			canvas.drawText("test", x+5, y+2, paint);
		}else{
			canvas.drawText(""+myHeaderCounter, x+5, y+2, paint);
		}
	}
/**
 * Prints a string at a particular point
 * @param x
 * @param y
 * @param graphName
 * @param canvas
 */
	public void drawCoord(int x, int y, String graphName, Canvas canvas) {
		Paint paint = new Paint();
		paint.setStyle(Paint.Style.FILL);
		paint.setAntiAlias(true);
		paint.setTextSize(12);
		paint.setColor(Color.WHITE);
		canvas.drawText(graphName, x+5, y+2, paint);
		
	}
		
}
